module MC.UI {
    export module muiDtPicker {
        /**
         * selectJq 需要绑定的对象字符串 
         */
        export function bind(selectJq: string) {
            loadmuiJsCss();
            let dateP = $(selectJq);
            for (let i = 0; i < dateP.length; i++) {
                let one = dateP[i];
                if (one.tagName !== 'INPUT') {
                    Object.defineProperty(one, "value", {
                        get: function () {
                            return one._value;
                        },
                        set: function (val) {
                            // console.log(val);
                            if (val == null || val == '') {
                                val = null;
                                $(one).text('未选择日期');
                                $(one).removeClass("tianxie");
                            } else {
                                $(one).text(val);
                                $(one).addClass("tianxie");
                            }
                            one._value = val;
                        },
                        enumerable: true,
                        configurable: true
                    });
                }


                $(one).text('未选择日期');
                $(one).addClass("muiDtPicker");
            }
            bindon(dateP);
        }
        function bindon(dateP: JQuery) {
            dateP.on('click', (e: any) => {
                let target = e.target;
                if (!target.DtPicker) {
                    target.DtPicker = new mui.DtPicker({ "type": "date","beginYear":1930,"endYear":2050 });
                }
                if (target.value) {
                    target.DtPicker.setSelectedValue(target.value);
                }
                target.DtPicker.show((val: any) => {
                    console.log(val);
                    target.value = val.value; 
                });
            });
        }

        let isload = false;  
        function loadmuiJsCss() {
            if (isload) {
                return;
            }
            MC.UI.Mui.loadbase(()=>{
                MC.UI.Mui.loadPicker();
            });
           
        }
    }
}